package com.d3works.apps.stock.rest;

import com.d3works.apps.base.db.Company;
import com.d3works.apps.base.db.Product;
import com.d3works.apps.base.db.ProductVariant;
import com.d3works.apps.base.db.ProductVariantAttr;
import com.d3works.apps.base.db.ProductVariantValue;
import com.d3works.apps.stock.db.StockLocation;
import com.d3works.apps.stock.rest.dto.StockProductResponse;
import com.d3works.apps.stock.rest.dto.StockProductVariantAttributeResponse;
import com.d3works.apps.stock.service.StockLocationService;
import com.d3works.apps.tool.api.ResponseConstructor;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.inject.Beans;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;

public class StockProductRestServiceImpl implements StockProductRestService {
  @Override
  public Response getProductIndicators(
      Product product, Company company, StockLocation stockLocation) throws D3worksException {
    Map<String, Object> stockIndicators;
    if (company == null) {
      stockIndicators =
          Beans.get(StockLocationService.class).getStockIndicators(product.getId(), 0L, 0L);
    } else if (stockLocation == null) {
      stockIndicators =
          Beans.get(StockLocationService.class)
              .getStockIndicators(product.getId(), company.getId(), 0L);
    } else {
      stockIndicators =
          Beans.get(StockLocationService.class)
              .getStockIndicators(product.getId(), company.getId(), stockLocation.getId());
    }

    return ResponseConstructor.build(
        Response.Status.OK,
        "Request completed",
        new StockProductResponse(product, stockIndicators));
  }

  @Override
  public List<StockProductVariantAttributeResponse> fetchAttributes(Product product)
      throws D3worksException {
    List<StockProductVariantAttributeResponse> attributes = new ArrayList<>();

    ProductVariant variant = product.getProductVariant();

    if (variant == null) {
      throw new D3worksException(
          TraceBackRepository.CATEGORY_MISSING_FIELD,
          "Product variant was not found and is required.");
    }

    if (variant.getProductVariantAttr1() != null && variant.getProductVariantValue1() != null) {
      ProductVariantAttr attr1 = variant.getProductVariantAttr1();
      ProductVariantValue value1 = variant.getProductVariantValue1();

      attributes.add(
          new StockProductVariantAttributeResponse(
              attr1.getName(),
              value1.getName(),
              value1.getPriceExtra(),
              value1.getApplicationPriceSelect()));
    }

    if (variant.getProductVariantAttr2() != null && variant.getProductVariantValue2() != null) {
      ProductVariantAttr attr2 = variant.getProductVariantAttr2();
      ProductVariantValue value2 = variant.getProductVariantValue2();

      attributes.add(
          new StockProductVariantAttributeResponse(
              attr2.getName(),
              value2.getName(),
              value2.getPriceExtra(),
              value2.getApplicationPriceSelect()));
    }

    if (variant.getProductVariantAttr3() != null && variant.getProductVariantValue3() != null) {
      ProductVariantAttr attr3 = variant.getProductVariantAttr3();
      ProductVariantValue value3 = variant.getProductVariantValue3();

      attributes.add(
          new StockProductVariantAttributeResponse(
              attr3.getName(),
              value3.getName(),
              value3.getPriceExtra(),
              value3.getApplicationPriceSelect()));
    }

    if (variant.getProductVariantAttr4() != null && variant.getProductVariantValue4() != null) {
      ProductVariantAttr attr4 = variant.getProductVariantAttr4();
      ProductVariantValue value4 = variant.getProductVariantValue4();

      attributes.add(
          new StockProductVariantAttributeResponse(
              attr4.getName(),
              value4.getName(),
              value4.getPriceExtra(),
              value4.getApplicationPriceSelect()));
    }

    if (variant.getProductVariantAttr5() != null && variant.getProductVariantValue5() != null) {
      ProductVariantAttr attr5 = variant.getProductVariantAttr5();
      ProductVariantValue value5 = variant.getProductVariantValue5();

      attributes.add(
          new StockProductVariantAttributeResponse(
              attr5.getName(),
              value5.getName(),
              value5.getPriceExtra(),
              value5.getApplicationPriceSelect()));
    }

    return attributes;
  }
}
